package xyz.xxpc.springcloud.test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPool {
    static int count = 0;
    public static void main(String[] args) {
        // 创建一个固定长度为2的线程池
        ExecutorService executor = Executors.newFixedThreadPool(2);
        for (int i = 0; i < 10; i++) {
            // 提交任务 -- 10个任务
            executor.submit(() -> {
                // 获取线程编号
                long id = Thread.currentThread().getId();
                System.out.println("thread id is: " + id + "name :" + Thread.currentThread().getName());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        System.out.println("任务提交完成");
        // 执行此函数后线程池不再接收新任务，并等待所有任务执行完毕后销毁线程
        executor.shutdown(); // 不执行此方法则 程序不会自动停止了
        // 爬虫?

    }
}
